#include <bits/stdc++.h>
using namespace std;

typedef long long LL;

const int N = 3e3 + 5,
          M = 1e6 * 2 + 50,
          INF = 0x3f3f3f3f;

struct Edge
{
    int to, next, w;
} e[M];
int ecnt;
int head[N], tail[N], link[N];

struct dsu
{
    int f[N];
    void init(int n)
    {
        for (int i = 0; i <= n; ++i)
            f[i] = i;
    }
    int find(int x)
    {
        return x == f[x] ? x : (f[x] = find(f[x]));
    }
    bool connect(int x, int y)
    {
        x = find(x);
        y = find(y);
        f[x] = y;
        return x != y;
    }

    bool test(int x, int y)
    {
        x = find(x);
        y = find(y);
        return x != y;
    }
} ds;

int n, m;
void init(int n)
{
    memset(head, -1, sizeof(int) * (n + 1));
    ds.init(n);
    ecnt = 0;
}

void add_edge(int u, int v, int w)
{
    e[ecnt] = {v, head[u], w};
    head[u] = ecnt++;
    e[ecnt] = {u, head[v], w};
    head[v] = ecnt++;
}

namespace stoerwagner
{
int mincutphase(int cnt, int &s, int &t)
{
    static bool vis[N];
    static int val[N];

    memset(vis + 1, 0, sizeof(vis[0]) * n);
    memset(val + 1, 0, sizeof(int) * n);

    priority_queue<pair<int, int>> que;
    t = 1;
    while (--cnt)
    {
        vis[s = t] = true;
        for (int u = s; ~u; u = link[u])
        {
            for (int p = head[u]; ~p; p = e[p].next)
            {
                int v = ds.find(e[p].to);
                if (!vis[v])
                {
                    que.push(make_pair(val[v] += e[p].w, v));
                }
            }
        }
        t = 0;
        while (!t)
        {
            if (que.empty())
                return 0; // not connected graph
            auto pa = que.top();
            que.pop();
            if (val[pa.second] == pa.first)
                t = pa.second;
        }
    }
    return val[t];
}
int go()
{
    int res = 0x7fffffff;
    for (int i = 1; i <= n; ++i)
    {
        tail[i] = i;
        link[i] = -1;
    }
    for (int i = n, s, t; i > 1; --i)
    {
        res = min(res, mincutphase(i, s, t));
        if (res == 0)
            break;
        if (s > t)
            swap(s, t);
        ds.connect(s, t);
        link[tail[t]] = s;
        tail[t] = tail[s];
    }
    return res;
}
} // namespace stoerwagner

int main(int argc, char const *argv[])
{
    while (~scanf("%d%d", &n, &m))
    {
        init(n);
        for (int i = 1; i <= m; ++i)
        {
            int u, v, w;
            scanf("%d%d%d", &u, &v, &w);
            if (u == v)
                continue;
            add_edge(u, v, w);
        }
        int ans = stoerwagner::go();
        printf("%d\n", ans);
    }

    // puts("1 2");
    return 0;
}